<?php
	if(empty($_GET["roomID"])) {
		die('Room ID cannot be empty');
	}
	
	$roomID = $_GET["roomID"];
	$studentID = $_GET["studentID"];
	
	$con = mysql_connect("localhost","root","");
	if (!$con) {
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("ciu196", $con);
	
	$sql = "SELECT * from Rooms WHERE roomID = '$roomID';";
	$res = mysql_query($sql,$con);
	if(!$res) {
		die('Error: ' . mysql_error());
	}
	
	$num_rows = mysql_num_rows($res);
	
	if($num_rows == 0) {	
		die('There is no room with that name' . $roomID);
	}
	
	// Checks if the user is already in the room
	$sql = "SELECT * FROM StudentsInRooms where roomID = '$roomID' AND studentID = '$studentID';";
	$res = mysql_query($sql,$con);
	if(!$res) {
		die('Error: ' . mysql_error());
	}
	
	$num_rows = mysql_num_rows($res);
		
	// If not in the room, then join the room
	if($num_rows == 0) {	
		$sql = "INSERT INTO StudentsInRooms VALUES('$studentID', '$roomID', 'ok')";
		if(!mysql_query($sql,$con)) {
			die('Error couldnt insert into StudentsInRooms: ' . mysql_error());
		}
		echo "Joined Room";
	} else {
		echo "The student is in the room";
	}
	
	mysql_close($con);	
?>